package com.sys.ai.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.ai.domain.AiModel;
import com.sys.ai.fo.AiModelQueryFO;
import com.sys.ai.mapper.AiModelMapper;
import com.sys.ai.service.IAiModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 模型Service业务层处理
 *
 * @author LGY
 * @date 2025-05-08
 */
@Slf4j
@Service
public class AiModelServiceImpl extends ServiceImpl<AiModelMapper, AiModel> implements IAiModelService {

    @Autowired
    private AiModelMapper aiModelMapper;

    /**
     * 查询模型
     *
     * @param id 模型主键
     * @return 模型
     */
    @Override
    public AiModel selectAiModelById(String id) {
        return this.getById(id);
    }

    /**
     * 查询模型列表
     *
     * @param aiModel 模型
     * @return 模型
     */
    @Override
    public List<AiModel> selectAiModelList(AiModel aiModel) {
        return aiModelMapper.selectAiModelList(aiModel);
    }

    /**
     * 新增模型
     *
     * @param aiModel 模型
     * @return 结果
     */

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertAiModel(AiModel aiModel) {
        this.save(aiModel);
    }

    /**
     * 修改模型
     *
     * @param aiModel 模型
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateAiModel(AiModel aiModel) {
        this.updateById(aiModel);
    }

    /**
     * 批量删除模型
     *
     * @param ids 需要删除的模型主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteAiModelByIds(String[] ids) {
        this.removeBatchByIds(Arrays.asList(ids));
    }

    @Override
    public List<AiModel> findAllEnableModel(AiModelQueryFO aiModelQueryFO) {
        LambdaQueryChainWrapper<AiModel> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(AiModel::getStatus, 0);
        if (!CollectionUtils.isEmpty(aiModelQueryFO.getModelType())) {
            queryChainWrapper.in(AiModel::getType, aiModelQueryFO.getModelType());
        }
        queryChainWrapper.orderByAsc(AiModel::getSorts);
        return queryChainWrapper.list();
    }

    @Override
    public AiModel getByCode(String modelCode) {
        List<AiModel> aiModels = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(AiModel::getCode, modelCode)
                .list();
        return CollectionUtils.isEmpty(aiModels) ? null : aiModels.get(0);
    }
}
